package com.sxbbc.common.core.service.system.solr;

import java.util.*;

import com.sxbbc.common.core.service.system.solr.entity.SolrProductDto;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.service.solr.SolrUtils;
import com.sxbbc.common.basics.service.solr.entity.Solrpage;
import com.sxbbc.common.basics.utils.maps.EntityUtils;
import org.springframework.util.CollectionUtils;

/**
 * 产品操作搜索引擎
 *
 * @author arno
 */
@Service
public class SolrproductService {

    @Autowired
    private HttpSolrClient httpSolrClient;

    /**
     * 添加产品
     *
     * @param dto 产品对象
     */
    public RestResponse addProduct(SolrProductDto dto) {
        try {
            SolrUtils<SolrProductDto> solrUtils = new SolrUtils<SolrProductDto>(httpSolrClient);
            solrUtils.addBean(dto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return GetRest.getSuccess("添加成功");
    }

    /**
     * 删除solr搜索库产品数据
     *
     * @param proCode 产品编码
     * @return
     */
    public RestResponse deleteProduct(String proCode) {
        SolrUtils<SolrProductDto> solrUtils = new SolrUtils<SolrProductDto>(httpSolrClient);
        solrUtils.deleteById(proCode);
        return GetRest.getSuccess("删除成功");
    }

    /**
     * 根据ID查询单个产品
     *
     * @param proCode 产品编码
     */
    public SolrProductDto selectById(String proCode) {
        SolrUtils<SolrProductDto> solrUtils = new SolrUtils<SolrProductDto>(httpSolrClient);
        String queryText = "id:" + proCode;
        SolrProductDto solrProductDto = new SolrProductDto();
        Solrpage<SolrProductDto> solrPage = solrUtils.selectById(queryText);
        for (SolrDocument document : solrPage.getResults()) {
            Map<String, Object> map = document.getFieldValueMap();
            solrProductDto = EntityUtils.mapToObject(map, SolrProductDto.class);
        }
        return solrProductDto;
    }

    /**
     * 产品高亮搜索
     *
     * @param solrProductDto 产品对象
     * @param sortType       排序类型 0:默认,1:价格,2:销售量,3:新品
     * @param sortDesc       asc 升序，desc降序
     * @param page           页码
     * @return
     */
    public Solrpage<SolrProductDto> queryProduct(SolrProductDto solrProductDto, Integer sortType, String sortDesc, int page) {
        SolrUtils<SolrProductDto> solrUtils = new SolrUtils<SolrProductDto>(httpSolrClient);
        SolrQuery query = new SolrQuery();
        String queryText = "*:*";
        //搜索条件
        if (!StringUtils.isEmpty(solrProductDto.getKeyword())) {
            queryText = "keyword:" + solrProductDto.getKeyword();
        }
        if (StringUtils.isNotBlank(solrProductDto.getPname())) {
            queryText = "pname:" + solrProductDto.getPname();
        }
        //过滤条件
        List<String> filterQueryList = new ArrayList<>();
        if (solrProductDto.getType() != null) {
            filterQueryList.add("type:" + solrProductDto.getType());
        }
        if (solrProductDto.getBid() != null) {
            filterQueryList.add("bid:" + solrProductDto.getBid());
        }
        if (solrProductDto.getFclassid() != null) {
            filterQueryList.add("fclassid:" + solrProductDto.getFclassid());
        }
        if (solrProductDto.getSclassid() != null) {
            filterQueryList.add("sclassid:" + solrProductDto.getSclassid());
        }
        if (solrProductDto.getTclassid() != null) {
            filterQueryList.add("tclassid:" + solrProductDto.getTclassid());
        }
        if (!CollectionUtils.isEmpty(filterQueryList)) {
            String[] toBeStored = filterQueryList.toArray(new String[filterQueryList.size()]);
            query.setFilterQueries(toBeStored);
        }
        //设置高亮 属性
        query.setParam("hl.fl", "pname", "descval");
        //设置排序
        ORDER px = ORDER.desc;
        if (!StringUtils.isBlank(sortDesc) && StringUtils.equals("asc", sortDesc)) {
            px = ORDER.asc;
        }
        if (sortType == null || sortType == 0) {
            query.set("sort", "sort " + px, "saleqty " + px, "proprice " + px);
        } else if (sortType == 1) {
            query.set("sort", "proprice " + px);
        } else if (sortType == 2) {
            query.set("sort", "saleqty " + px);
        } else if (sortType == 3) {
            query.set("sort", "stime " + px);
        } else if (sortType == 4) {
            query.set("sort", "point " + px);
        }
        Solrpage<SolrProductDto> solrPage = solrUtils.queryList(query, queryText, page, SolrProductDto.class);
        List<SolrProductDto> list = new ArrayList<>();
        //组合List
        Map<String, Map<String, List<String>>> highlighting = solrPage.getHighlighting();
        for (SolrDocument document : solrPage.getResults()) {
            Map<String, Object> map = document.getFieldValueMap();
            SolrProductDto comInfo = EntityUtils.mapToObject(map, SolrProductDto.class);
            //设置高亮
			/*if(highlighting!=null) {
				Object id = document.get("id");
				Map<String, List<String>> fieldMap = highlighting.get(id);
				if(fieldMap!=null) {
					List<String> pname = fieldMap.get("pname");
					if(pname!=null) {
						comInfo.setPname(pname.get(0));
					}
					List<String> desc = fieldMap.get("descval");
					if(desc!=null) {
						comInfo.setDescval(desc.get(0));
					}
				}
			}*/
            list.add(comInfo);
        }
        solrPage.setList(list);
        return solrPage;
    }
    
    /**
     * 产品高亮搜索
     *
     * @param solrProductDto 产品对象
     * @param sortType       排序类型 0:默认,1:价格,2:销售量,3:新品
     * @param sortDesc       asc 升序，desc降序
     * @param page           页码
     * @return
     */
    public Solrpage<SolrProductDto> queryProduct(SolrProductDto solrProductDto, Integer sortType, String sortDesc, int page, Integer size) {
        SolrUtils<SolrProductDto> solrUtils = new SolrUtils<SolrProductDto>(httpSolrClient);
        SolrQuery query = new SolrQuery();
        String queryText = "*:*";
        //搜索条件
        if (!StringUtils.isEmpty(solrProductDto.getKeyword())) {
            queryText = "keyword:" + solrProductDto.getKeyword();
        }
        if (StringUtils.isNotBlank(solrProductDto.getPname())) {
            queryText = "pname:" + solrProductDto.getPname();
        }
        //过滤条件
        List<String> filterQueryList = new ArrayList<>();
        if (solrProductDto.getType() != null) {
            filterQueryList.add("type:" + solrProductDto.getType());
        }
        if (solrProductDto.getBid() != null) {
            filterQueryList.add("bid:" + solrProductDto.getBid());
        }
        if (solrProductDto.getFclassid() != null) {
            filterQueryList.add("fclassid:" + solrProductDto.getFclassid());
        }
        if (solrProductDto.getSclassid() != null) {
            filterQueryList.add("sclassid:" + solrProductDto.getSclassid());
        }
        if (solrProductDto.getTclassid() != null) {
            filterQueryList.add("tclassid:" + solrProductDto.getTclassid());
        }
        if (!CollectionUtils.isEmpty(filterQueryList)) {
            String[] toBeStored = filterQueryList.toArray(new String[filterQueryList.size()]);
            query.setFilterQueries(toBeStored);
        }
        //设置高亮 属性
        query.setParam("hl.fl", "pname", "descval");
        //设置排序
        ORDER px = ORDER.desc;
        if (!StringUtils.isBlank(sortDesc) && StringUtils.equals("asc", sortDesc)) {
            px = ORDER.asc;
        }
        if (sortType == null || sortType == 0) {
            query.set("sort", "sort " + px, "saleqty " + px, "proprice " + px);
        } else if (sortType == 1) {
            query.set("sort", "proprice " + px);
        } else if (sortType == 2) {
            query.set("sort", "saleqty " + px);
        } else if (sortType == 3) {
            query.set("sort", "stime " + px);
        } else if (sortType == 4) {
            query.set("sort", "point " + px);
        }
        Solrpage<SolrProductDto> solrPage = solrUtils.queryList(query, queryText, page, size, SolrProductDto.class);
        List<SolrProductDto> list = new ArrayList<>();
        //组合List
        Map<String, Map<String, List<String>>> highlighting = solrPage.getHighlighting();
        for (SolrDocument document : solrPage.getResults()) {
            Map<String, Object> map = document.getFieldValueMap();
            SolrProductDto comInfo = EntityUtils.mapToObject(map, SolrProductDto.class);
            //设置高亮
			/*if(highlighting!=null) {
				Object id = document.get("id");
				Map<String, List<String>> fieldMap = highlighting.get(id);
				if(fieldMap!=null) {
					List<String> pname = fieldMap.get("pname");
					if(pname!=null) {
						comInfo.setPname(pname.get(0));
					}
					List<String> desc = fieldMap.get("descval");
					if(desc!=null) {
						comInfo.setDescval(desc.get(0));
					}
				}
			}*/
            list.add(comInfo);
        }
        solrPage.setList(list);
        return solrPage;
    }
}
